/* 
 *  Copyright (C) 2006 James Young
 * `gurumind(a)gmail.com
 * 
 */

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace IPDEditor
{
    public partial class IPDEditorGeneric : IPDEditor.IPDEditorBase
    {
        public IPDUIField ipdUIField;
        Dictionary<string, object> _dataArray;
        TextBox[] tbList;

        Point ptOrg = new Point(15, 45);
        int txtPos = 120;
        int vSpace = 30;
        Size txtSize = new Size(200, 21);

        public IPDEditorGeneric()
        {
            InitializeComponent();
        }

        public override void FillData(IPDField field)
        {
            _dataArray = new Dictionary<string, object>();
            if (field is IPDFieldSBAppData)
            {
                IPDFieldSBAppData apd = (IPDFieldSBAppData)field;
                if (apd.SBType == "BrowserConfig")
                {
                    IPDFieldSBAppDataForBrowser bcd = (IPDFieldSBAppDataForBrowser)apd.child;
                    bcd.Parse(apd.appData);
                    _dataArray.Add("Home Address", bcd.homeAddress);
                    _dataArray.Add("Data Source", bcd.dataSource);
                    _dataArray.Add("Data Source UID", bcd.dsUID);
                    _dataArray.Add("Name", bcd.name);
                }
            }
            else
            {
            }

            Create();
        }

        protected override void Save()
        {
            if (ipdUIField.field is IPDFieldSBAppData)
            {
                IPDFieldSBAppData apd = (IPDFieldSBAppData)ipdUIField.field;
                if (apd.SBType == "BrowserConfig")
                {
                    IPDFieldSBAppDataForBrowser bcd = (IPDFieldSBAppDataForBrowser)apd.child;
                    bcd.homeAddress = tbList[0].Text;
                    bcd.dataSource = tbList[1].Text;
                    bcd.dsUID = tbList[2].Text;
                    bcd.name = tbList[3].Text;
                }
                else
                    base.Save();
            }
            else
                base.Save();
        }

        private void Create()
        {
            //Controls.Clear();

            tbList = new TextBox[_dataArray.Count];
            int i = 0;
            foreach(KeyValuePair<string, object> kvp in _dataArray)
            {
                Label lbl = new Label();
                lbl.Text = kvp.Key;
                lbl.Location = new Point(ptOrg.X, ptOrg.Y + vSpace * i);
                Controls.Add(lbl);

                TextBox tb = new TextBox();
                tb.Text = (string)kvp.Value;
                tb.Location = new Point(txtPos, ptOrg.Y + vSpace * i);
                tb.Size = txtSize;
                Controls.Add(tb);

                tbList[i] = tb;
                i++;
            }
        }
    }
}

